import { supabase } from '../config/supabase'
import Mock from 'mockjs'

// 模拟课程数据
const mockCourses = [
  {
    id: 1,
    name: '基础数学',
    description: '基础数学课程，包含算术、代数等内容',
    duration: 30,
    points: 100,
    level: '初级'
  },
  {
    id: 2,
    name: '进阶数学',
    description: '进阶数学课程，包含几何、微积分等内容',
    duration: 45,
    points: 200,
    level: '中级'
  },
  {
    id: 3,
    name: '高等数学',
    description: '高等数学课程，包含线性代数、概率论等内容',
    duration: 60,
    points: 300,
    level: '高级'
  },
  {
    id: 4,
    name: '英语基础',
    description: '英语基础课程，包含词汇、语法等内容',
    duration: 30,
    points: 100,
    level: '初级'
  },
  {
    id: 5,
    name: '英语进阶',
    description: '英语进阶课程，包含阅读、写作等内容',
    duration: 45,
    points: 200,
    level: '中级'
  }
]

// 模拟学生课程分配数据
const mockStudentCourses = {}

// 获取所有课程
export const getAllCourses = async () => {
  await new Promise(resolve => setTimeout(resolve, 500))
  return mockCourses
}

// 获取学生课程
export const getStudentCourses = async (studentId) => {
  await new Promise(resolve => setTimeout(resolve, 500))
  return mockStudentCourses[studentId] || []
}

// 分配课程给学生
export const assignCoursesToStudent = async (studentId, courses) => {
  await new Promise(resolve => setTimeout(resolve, 500))
  
  // 检查是否已分配过课程
  if (mockStudentCourses[studentId]?.length > 0) {
    throw new Error('该学生已分配了部分课程，请勿重复分配')
  }
  
  // 分配课程
  mockStudentCourses[studentId] = courses.map(courseId => 
    mockCourses.find(c => c.id === courseId)
  )
  
  return true
}

// 批量分配课程
export const batchAssignCourses = async (studentIds, courses) => {
  await new Promise(resolve => setTimeout(resolve, 500))
  
  for (const studentId of studentIds) {
    // 检查是否已分配过课程
    if (mockStudentCourses[studentId]?.length > 0) {
      throw new Error(`学生ID ${studentId} 已分配了部分课程，请勿重复分配`)
    }
    
    // 分配课程
    mockStudentCourses[studentId] = courses.map(courseId => 
      mockCourses.find(c => c.id === courseId)
    )
  }
  
  return true
}

// 获取课程选项
export const getCourseOptions = () => {
  return mockCourses.map(course => ({
    value: course.id,
    label: course.name
  }))
}

export default {
  getAllCourses,
  getStudentCourses,
  assignCoursesToStudent,
  batchAssignCourses,
  getCourseOptions
}
